package com.njcc.control;

import com.google.gson.JsonParser;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.njcc.util.DBCon;
import com.njcc.util.JsonConfigUtil;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * 专门用来处理打印请求的servlet
 */
@WebServlet("/printServlet")
public class printServlet extends HttpServlet {
    private static final Logger logger = LogManager.getLogger(printServlet.class);
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String body = IOUtils.toString(request.getInputStream());
        logger.info("body:"+body);
        logger.info("fileName:"+new JsonParser().parse(body).getAsJsonObject().get("fileName").getAsString());
        toPrint(new JsonParser().parse(body).getAsJsonObject().get("fileName").getAsString());
        logger.info("printServlet打印成功");
    }
    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request, response);
    }
    
    private void toPrint(String name){
        
        String path= DBCon.file_Path+name;
        logger.info("开始打印");
        logger.info("文件路径:"+ path);
        logger.info("打印机的名字:"+JsonConfigUtil.getJsonConfigValue("printer-name"));
        try {
            ComThread.InitSTA();
            logger.info("InitSTA success");
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e);
        }
        ActiveXComponent word=new ActiveXComponent("Word.Application");
        logger.info("ActiveXComponent:"+word);
        word.setProperty("ActivePrinter", new Variant(JsonConfigUtil.getJsonConfigValue("printer-name")));
        Dispatch.put(word, "Visible", new Variant(false));
        Dispatch docs=word.getProperty("Documents").toDispatch();
        logger.info("docs:"+docs);
        Dispatch doc=Dispatch.call(docs, "Open", path).toDispatch();
        logger.info("doc:"+doc);
        try {
            Dispatch.call(doc, "PrintOut");//执行打印
            logger.info("打印成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("打印失败");
            logger.info(e.toString());
        }finally{
            try {
                if(doc!=null){
                    Dispatch.call(doc, "Close",new Variant(0));
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            //释放资源
            ComThread.Release();
        }
    }
}
